package com.sign2024.sign2024project.capitalconstruction.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.param.PageParam;
import com.sign2024.sign2024core.entry.capitalconstruction.JjHistoryData;
import com.sign2024.sign2024project.capitalconstruction.service.JjHistoryDataService;
import com.sign2024.sign2024project.goodsmaterials.vo.WzHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author yujie
 * @data 2024/11/18 8:23
 */
@Api(tags = "基建")
@RestController
@RequestMapping("/api/JjHistoryData")
public class JjHistoryDataController {

    @Resource
    private JjHistoryDataService jjHistoryDataService;

    @Log
    @Transactional
    @PostMapping("/import")
    public CommonResult<JjHistoryData> importInfo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            List<JjHistoryData> list =
                    EasyExcel.read(inputStream).head(JjHistoryData.class).sheet().doReadSync();
            jjHistoryDataService.exportData(list);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "基建", businessType = "查询")
    @ApiOperation(value = "1分页基建历史数据", notes = "1分页基建历史数据")
    @PostMapping("/page")
    public CommonResult<JjHistoryData> page(PageParam param) {
        Page<JjHistoryData> page = jjHistoryDataService.pageJjHistoryData(param);
        return CommonResult.success(page);
    }

    @Log(title = "基建", businessType = "查询")
    @ApiOperation(value = "1查询标段历史频次", notes = "1查询标段历史频次")
    @PostMapping("/select")
    public CommonResult<WzHistoryVo> selectVo(@RequestParam @ApiParam("标段名称") String sectionName,
                                              @RequestParam @ApiParam("频次") Integer skillNum) {
        List<WzHistoryVo> page = jjHistoryDataService.selectJjHistoryVo(sectionName, skillNum);
        return CommonResult.success(page);
    }

    @Log
    @Transactional
    @ApiOperation(value = "1基建历史抽取方案excel导入", notes = "1基建历史抽取方案excel导入")
    @PostMapping("/importJjHistory")
    public CommonResult<JjHistoryData> importProject(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            List<JjHistoryData> list =
                    EasyExcel.read(inputStream).head(JjHistoryData.class).sheet().doReadSync();
            jjHistoryDataService.importJjHistory(list);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }
}
